﻿using System;
using System.Diagnostics;
using System.Threading;
using System.Windows.Input;

namespace MedLink2011.Core {

    public class RelayCommand : ICommand
    {
        // Fields
        private readonly Func<bool> _canExecute;
        private readonly Action _execute;
        private EventHandler CanExecuteChanged;

        private readonly Action _handler;
        private bool _isEnabled;


        //public RelayCommand(Action handler)
        //{
        //    _handler = handler;
        //}
        // Methods
        public RelayCommand(Action execute): this(execute, null)
        {
        }

        public RelayCommand(Action execute, Func<bool> canExecute)
        {
            if (execute == null)
            {
                throw new ArgumentNullException("execute");
            }
            this._execute = execute;
            this._canExecute = canExecute;
        }

        [DebuggerStepThrough]
        bool ICommand.CanExecute(object parameter)
        {
            return ((this._canExecute == null) ? true : this._canExecute());
        }


        public bool IsEnabled
        {
            get { return _isEnabled; }
            set
            {
                if (value != _isEnabled)
                {
                    _isEnabled = value;
                    if (CanExecuteChanged != null)
                    {
                        CanExecuteChanged(this, EventArgs.Empty);
                    }
                }
            }
        }

        public bool CanExecute(object parameter)
        {
            return IsEnabled;
        }

        // Events
        event EventHandler ICommand.CanExecuteChanged
        {
            add
            {
                EventHandler handler2;
                EventHandler canExecuteChanged = this.CanExecuteChanged;
                do
                {
                    handler2 = canExecuteChanged;
                    EventHandler handler3 = (EventHandler) Delegate.Combine(handler2, value);
                    canExecuteChanged = Interlocked.CompareExchange<EventHandler>(ref this.CanExecuteChanged, handler3,
                                                                                  handler2);
                } while (canExecuteChanged != handler2);
            }
            remove
            {
                EventHandler handler2;
                EventHandler canExecuteChanged = this.CanExecuteChanged;
                do
                {
                    handler2 = canExecuteChanged;
                    EventHandler handler3 = (EventHandler) Delegate.Remove(handler2, value);
                    canExecuteChanged = Interlocked.CompareExchange<EventHandler>(ref this.CanExecuteChanged, handler3,
                                                                                  handler2);
                } while (canExecuteChanged != handler2);
            }
        }


        public void RaiseCanExecuteChanged()
        {
            EventHandler handler = this.CanExecuteChanged;
            if (handler != null)
            {
                handler(this, EventArgs.Empty);
            }
        }


        public void Execute(object parameter)
        {
            this._execute();
        }




    }

    public class RelayCommand<T> : ICommand
    {
        // Fields
        private readonly Predicate<T> _canExecute;
        private readonly Action<T> _execute;
        private EventHandler CanExecuteChanged;
        private bool _isEnabled;

        // Events
        public bool IsEnabled
        {
            get { return _isEnabled; }
            set
            {
                if (value != _isEnabled)
                {
                    _isEnabled = value;
                    if (CanExecuteChanged != null)
                    {
                        CanExecuteChanged(this, EventArgs.Empty);
                    }
                }
            }
        }

        event EventHandler ICommand.CanExecuteChanged
        {
            add
            {
                EventHandler handler2;
                EventHandler canExecuteChanged = this.CanExecuteChanged;
                do
                {
                    handler2 = canExecuteChanged;
                    EventHandler handler3 = (EventHandler) Delegate.Combine(handler2, value);
                    canExecuteChanged = Interlocked.CompareExchange<EventHandler>(ref this.CanExecuteChanged, handler3,
                                                                                  handler2);
                } while (canExecuteChanged != handler2);
            }
            remove
            {
                EventHandler handler2;
                EventHandler canExecuteChanged = this.CanExecuteChanged;
                do
                {
                    handler2 = canExecuteChanged;
                    EventHandler handler3 = (EventHandler) Delegate.Remove(handler2, value);
                    canExecuteChanged = Interlocked.CompareExchange<EventHandler>(ref this.CanExecuteChanged, handler3,
                                                                                  handler2);
                } while (canExecuteChanged != handler2);
            }
        }

        // Methods
        public RelayCommand(Action<T> execute)
            : this(execute, null)
        {
        }

        public RelayCommand(Action<T> execute, Predicate<T> canExecute)
        {
            if (execute == null)
            {
                throw new ArgumentNullException("execute");
            }
            this._execute = execute;
            this._canExecute = canExecute;
        }

        public bool CanExecute(object parameter)
        {
            return ((this._canExecute == null) || this._canExecute((T)parameter));
        }

        public void Execute(object parameter)
        {
            this._execute((T)parameter);
        }

        public void RaiseCanExecuteChanged()
        {
            EventHandler handler = this.CanExecuteChanged;
            if (handler != null)
            {
                handler(this, EventArgs.Empty);
            }
        }
    }




}